package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Role;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import com.atguigu.base.BaseController;

/**
 * @author 杨林
 * @create 2022-09-13 15:05 星期二
 * description:
 */
@Controller
@RequestMapping("role")
public class RoleController extends BaseController {

    @Reference
    private RoleService roleService;

    /*@RequestMapping
    public String index(ModelMap model) {
        List<Role> list = roleService.findAll();

        model.addAttribute("list", list);
        return "role/index";
    }*/

    /**
     * 实现角色的查询功能
     * @param request
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.show')")
    @RequestMapping
    public String findPage(HttpServletRequest request,ModelMap model){
        Map<String, Object> filters = getFilters(request);

        //调用业务层获取分页相关的数据和参数
        PageInfo<Role> page = roleService.findPage(filters);

        model.addAttribute("page",page);
        model.addAttribute("filters", filters);

        return "role/index";
    }





    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/create")
    public String create(){
        return "role/create";
    }

    @PreAuthorize("hasAnyAuthority('role.create')")
    @RequestMapping("/save")
    public String save(Role role){
        roleService.insert(role);
        return "common/successPage";
    }

    @PreAuthorize("hasAnyAuthority('role.edit')")
    @GetMapping("edit/{id}")
    public String getById(@PathVariable Long id, Model model){
        Role role = roleService.getById(id);
        model.addAttribute("role",role);
        return "role/edit";
    }

    @PreAuthorize("hasAnyAuthority('role.edit')")
    @PostMapping("/update")
    public String update(Role role){

        roleService.update(role);

        return "common/successPage";
    }

    @PreAuthorize("hasAnyAuthority('role.delete')")
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){

        roleService.delete(id);
        return  "redirect:/role";
    }

    @Reference
    private PermissionService permissionService;

    /**
     * 进入到分配权限的页面
     * @param roleId
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.assgin')")
    @RequestMapping("/assignShow/{roleId}")
    public String assignShow(@PathVariable() Long roleId,ModelMap model){
        List<Map<String, Object>> zNodes = permissionService.findPermissionsByRoleId(roleId);
        model.addAttribute("zNodes", zNodes);
        model.addAttribute("roleId",roleId);
        return "role/assignShow";
    }

    /**
     * 给角色分配权限 并存入数据库
     * @param roleId
     * @param permissionIds
     * @return
     */
    @PreAuthorize("hasAnyAuthority('role.assgin')")
    @RequestMapping("/assignPermission")
    public String assignPermission(Long roleId,Long[] permissionIds){
        permissionService.saveRolePermissionRealtionShip(roleId,permissionIds);
        return "common/successPage";
    }

}
